$(function() {
	var dom = document.getElementById("container");
	var myChart = echarts.init(dom);
	var app = {};
	option = null;

	var planePath = 'path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.063c0.521-84.662-26.629-121.796-63.961-121.491c-37.332-0.305-64.482,36.829-63.961,121.491l0.073,208.063l-319.9,221.799v89.254l330.343-157.288l12.238,241.308l-134.449,92.931l0.531,42.034l175.125-42.917l175.125,42.917l0.531-42.034l-134.449-92.931l12.238-241.308L1705.06,1318.313z';

	var convertData = function(data) {
		var res = [];
		for (var i = 0; i < data.length; i++) {
			var dataItem = data[i];
			var fromCoord = geoCoordMap[dataItem[0].name];
			var toCoord = geoCoordMap[dataItem[1].name];
			if (fromCoord && toCoord) {
				res.push({
					fromName : dataItem[0].name,
					toName : dataItem[1].name,
					coords : [ fromCoord, toCoord ]
				});
			}
		}
		return res;
	};

	$.get("/ExternalTable/airline",{'isInt':'Y'}, function(result) {
		var obj = JSON.parse(result);
		var QDchufa = [];
		var QDdaoda = [];

		var inlist = obj.inList;
		var outlist = obj.outList;

		// QDchufa
		for (var i = 0; i < outlist.length; i++) {
			var name = outlist[i].name;
			var value = outlist[i].value;

			var from = {
				name : '青岛'
			};
			var to = {};
			to['name'] = name;
			to['value'] = value;

			var tmpList = [];
			tmpList.push(from);
			tmpList.push(to);
			QDchufa.push(tmpList);
		}

		// QDdaoda
		for (var i = 0; i < inlist.length; i++) {
			var name = inlist[i].name;
			var value = inlist[i].value;

			var from = {};
			from['name'] = name;
			from['value'] = value;

			var to = {};
			to['name'] = '青岛';

			var tmpList = [];
			tmpList.push(from);
			tmpList.push(to);
			QDdaoda.push(tmpList);
		}
		console.log(QDdaoda);

		// initialize chart
		var color = [ '#a6c84c', '#ffa022' ];
		// var color = [ '#A52A2A', '#ffa022' ];
		var series = [];
		
		[ [ '出发', QDchufa ],[ '到达', QDdaoda ]  ].forEach(function(item, i) {
			series.push({
				name : '青岛' + item[0],
				type : 'lines',
				zlevel : 1,
				effect : {
					show : true,
					period : 6,
					trailLength : 0.7,
					color : '#fff',
					symbolSize : 3
				},
				lineStyle : {
					normal : {
						color : color[i],
						width : 0,
						curveness : 0.2
					}
				},
				data : convertData(item[1])
			}, {
				name : '青岛' + item[0],
				type : 'lines',
				zlevel : 2,
				symbol : [ 'none', 'arrow' ],
				symbolSize : 10,
				effect : {
					show : true,
					period : 6,
					trailLength : 0,
					symbol : planePath,
					symbolSize : 15
				},
				lineStyle : {
					normal : {
						color : color[i],
						width : 1,
						opacity : 0.6,
						curveness : 0.2
					}
				},
				data : convertData(item[1])
			}, {
				name : '青岛' + item[0],
				type : 'effectScatter',
				coordinateSystem : 'geo',
				zlevel : 2,
				rippleEffect : {
					brushType : 'stroke'
				},
				label : {
					normal : {
						show : true,
						position : 'right',
						formatter : '{b}'
					}
				},
				symbolSize : function(val) {
					return val[2] / 8;
				},
				itemStyle : {
					normal : {
						color : color[i]
					}
				},
				data : item[1].map(function(dataItem) {
					return {
						name : dataItem[1-i].name,
						value : geoCoordMap[dataItem[1-i].name]
								.concat([ dataItem[1-i].value ])
					};
				})
			});
		});
					
		option = {
			backgroundColor : '#404a59',
			// backgroundColor : '#AEEEEE',
			title : {
				text : '今日国际通行航线一览',
				subtext : new Date(),
				left : 'center',
				textStyle : {
					color : '#fff'
				}
			},
			tooltip : {
				trigger : 'item'
			},
			legend : {
				orient : 'vertical',
				top : 'top',
				left : 'right',
				data : [ '青岛出发', '青岛到达' ],
				textStyle : {
					color : '#fff'
				},
				selectedMode : 'single'
			},
			geo : {
				map : 'world',
				label : {
					emphasis : {
						show : false
					}
				},
				roam : true,
				itemStyle : {
					normal : {
						areaColor : '#323c48',
						// areaColor : '#6CA6CD',
						borderColor : '#404a59'
					},
					emphasis : {
						areaColor : '#2a333d'
					}
				}
			},
			series : series
		};
		;
		if (option && typeof option === "object") {
			myChart.setOption(option, true);
		}
	});

});
